package com.qf.java2107.yb.hotel.dao.impl;

import com.qf.java2107.yb.hotel.dao.TableDao;
import com.qf.java2107.yb.hotel.pojo.Table;
import com.qf.java2107.yb.hotel.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * 餐桌持久层实现
 * @author YB
 * @date 2021/12/16
 * @VERSION V1.0
 **/
public class TableDaoImpl implements TableDao {

    private QueryRunner queryRunner = null;

    @Override
    public List<Table> findLikeName(String tableName) throws SQLException {
        queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql="SELECT table_id tableId,table_name tableName,table_status tableStatus,reservation_time time FROM t_dinner_table WHERE table_name LIKE ?";
        return queryRunner.query(sql,new BeanListHandler<>(Table.class),tableName);
    }

    @Override
    public int save(Table table) throws SQLException {
        queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql="INSERT INTO t_dinner_table(table_name,table_status,reservation_time) VALUES(?,?,?)";
        return queryRunner.update(sql,table.getTableName(),table.getTableStatus(),table.getReserVationTime());
    }

    @Override
    public int update(Table table) throws SQLException {
        queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql="UPDATE t_dinner_table SET table_status=? WHERE table_id=?";
        return queryRunner.update(sql,table.getTableStatus(),table.getTableId());
    }

    @Override
    public int delete(Integer tableId) throws SQLException {
        queryRunner = new QueryRunner(JdbcUtils.getDataSource());
        String sql="DELETE FROM t_dinner_table WHERE table_id=?";
        return queryRunner.update(sql,tableId);
    }
}
